package save;

import fj.F;
import fj.data.Option;
import global.Global;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import server.Initialize;
import server.db.DatabaseFunctor;
import server.db.StmtHelper;
import server.objects.User;

import ajaxmethods.SessionMethod;

public class Form extends SessionMethod {

	public static String url() {
		return Initialize.appURL()+"/ajaxCall.jsp?do="+Form.class.getCanonicalName();
	}
	
	Integer idform;
	
	private F<Integer, DatabaseFunctor<Integer>> addContent = new F<Integer, DatabaseFunctor<Integer>>() {

		@Override
		public DatabaseFunctor<Integer> f(Integer formid) {
			idform=formid;
			link = Initialize.appURL()+"/act.jsp?do=external.SubmitForm&id="+formid;
			String formContent ="<form action=\""+Initialize.appURL()+"/act.jsp?do=external.SubmitForm&id="+formid+"\">"+content+"</form>";
			return StmtHelper.db("update form set content=").addVariable(formContent).addSql(", link=").addVariable(link)
				.addSql(" where idform=").addVariable(formid).executeUpdate();
		}
		
	};
	
	String content;
	String link;
	
	@Override
	public void perform(HttpServletRequest request,HttpServletResponse response)
	{
		super.perform(request, response);
		String formtitle = request.getParameter("formtitle");
		content = request.getParameter("content");
		
		Global global = new Global(request);
		User u = global.getUser();
		try {
			StmtHelper.db("insert into form (title,iduser) values (").addVariable(formtitle)
				.addSql(",").addVariable(u.id).addSql(")").executeUpdate().provideTo(addContent).unsafeUpdate();
			StringBuffer sb = new StringBuffer();
			sb.append("<div id=\""+idform+"\" class=\"row\"><div class=\"codecontent\" style=\"display:none;\">"+content+"</div>");
			sb.append("<div class=\"rowitem\" style=\"width:128px;text-align:left;\"><img class=\"editformname\" src=\""+Initialize.appURL()+"/images/icons/edit.png\"></img><a class=\"editformname\">Name Your Form</a>" +
					"<div style=\"display:none;\" class=\"textbox\"><input class=\"esoinput\" maxlength=\"35\" id=\"formname\">" +
					"<button style=\"width:42px;\" name=\"submit\">save</button><button style=\"width:52px;\" name=\"cancel\">cancel</button></div></div>");
			sb.append("<div class=\"rowitem\">"+Option.fromString(formtitle).orSome("")+"</div>");
			sb.append("<div class=\"rowitem\">0</div>");
			sb.append("<div class=\"rowitem\">0</div>");
			sb.append("<div class=\"rowitem\"><div extraheight=\"205\" class=\"codeBubble\" id=\"detailview\"><a id=\""+idform+"\" class=\"detailLink moredetails\">Get Code</a></div></div>");
			sb.append("<div style=\"width:80px;\" class=\"rowitem\"><img extraheight=\"130\" class=\"delete\" src=\""+Initialize.appURL()+"/images/icons/delete.png\"/></div>");
			sb.append("</div>");
			data.put("content", content).put("link",link).put("addrow", sb.toString()).put("idform",idform).write(response.getWriter());
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}
